<?php
namespace app\teacher\controller;
use app\common\lib;

class Index extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    public function home()
    {
        return $this->fetch();
    }

    /**
     * 修改个人头像
     * @return Json
     */
    public function change_face()
    {
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return Json
     */
    public function change_password()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            if (!captcha_check($data['code'])) {
                return show('200','验证码错误','0');
            }
            $isPassword = lib\Password::isPassword($data['password'],$data['rpassword']);
            if(!$isPassword) {
                return show('200','两次密码输入不一致','0');
            }
            try{
                $res = $this->_user->save(['password'=>lib\Password::setPassword($data['password'])],['id'=>$this->isLogin()['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$res) {
                return show('200','与原密码一致，未更改数据','0');
            }
            $_login_msg = lib\Tools::get_login_msg($this->isLogin()['id'],'1','修改密码成功');
            $this->_user_login_info->save($_login_msg);
            return show('200','修改密码成功，重启生效','1');
        }else {
            return $this->fetch();
        }
    }

    /**
     * 修改个人资料
     * @return mixed|\think\response\Json
     */
    public function person()
    {
        if(request()->isAjax()){
            $data = input('post.');
            if (!captcha_check($data['code'])) {
                return show('200','验证码错误','0');
            }
            try{
                $this->_teacher->allowField(true)->save($data,['uid'=>$this->isLogin()['id']]);
            }catch (\Exception $e) {
                return show('500',$e->getMessage(),'0');
            }
            return show('200','更新成功，点击确定生效','1');
        }else {
            $data = $this->_teacher->get(['uid' => $this->isLogin()['id']]);
            $this->assign([
                'data' => $data
            ]);
            return $this->fetch();
        }
    }
}